package com.xxxx.server.controller;

import com.xxxx.server.exceptions.MyException;
import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.Role;
import com.xxxx.server.pojo.param.AdminParam;
import com.xxxx.server.pojo.param.BackPasswordParam;
import com.xxxx.server.pojo.param.UpdUserParam;
import com.xxxx.server.service.IAdminService;
import com.xxxx.server.service.IRoleService;
import com.xzl.tool.util.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2020-10-15
 */
@RestController
@Api(tags = "用户管理AdminController")
public class AdminController {

    @Resource
    private IAdminService adminService;
    @Resource
    private IRoleService roleService;

    @ApiOperation(value = "获取所有操作员")
    @GetMapping("/admin")
    public List<Admin> getAllAdmins(String data) {
        return adminService.getAllAdmins(data);
    }

//    @GetMapping("system/admin")
//    @ApiOperation("根据姓名模糊查询")
//    public List<Admin> getAdminList(String keywords) {
//        return adminService.getAdminsByNameLike(keywords);
//    }

    /*@GetMapping("/system/admin/roles")
    @ApiOperation("获得所有角色列表")
    public List<Role> getAllRoles() {
        return roleService.getRolesAll();
    }

    @ApiOperation("修改操作员角色")
    @PutMapping("/system/admin/role")
    public RespBean updateRoles(Integer adminId, String rids) {
        System.out.println("rids:" + rids);
        return adminService.updateRolesById(adminId, rids);
    }

    @ApiOperation("修改启用状态")
    @PutMapping("/system/admin/")
    public RespBean updateEnabled(@RequestBody Map<String, Object> admin) {
        boolean enabled = (boolean) admin.get("enabled");
        Integer adminId = (Integer) admin.get("id");
        //若enabled为true
        if (enabled) {
            return adminService.updateEnableStatus(1, adminId);
        } else {
            return adminService.updateEnableStatus(0, adminId);
        }
    }*/

    @ApiOperation(value = "修改个人信息")
    @PutMapping("/admin/info")
    public RespBean updateAdminInfo(@RequestBody UpdUserParam admin) {
        return adminService.updateAdminInfo(admin);
    }


    @ApiOperation("获取当前登录用户信息")
    @GetMapping("/admin/info")
    public RespBean getAdminInfo(Principal principal) {
        if (null == principal)
            return null;
        String username = principal.getName();
        Admin admin = adminService.getUserByUsername(username);
        admin.setPassword(null);
        admin.setRoles(adminService.getRoles(admin.getId()));
        return RespBean.success(admin);
    }

    @ApiOperation(value = "删除操作员")
    @DeleteMapping("/system/admin/{id}")
    public RespBean deleteAdminById(@PathVariable Integer id){
        return adminService.deleteAdminById(id);
    }
//
//    @ApiOperation(value = "修改密码")
//    @PutMapping("/admin/pass")
//    public RespBean updatePassword(@RequestBody Map<String, Object> ruleForm){
//        return adminService.updatePwdById(ruleForm);
//    }

    @ApiOperation("修改头像")
    @PostMapping("/admin/userface")
    public RespBean changeUserFace(@Param("file") MultipartFile file, Principal principal) {
        if (null == principal)
            return null;
        String username = principal.getName();
        return adminService.changeUserFace(file,username);
    }

    @ApiOperation("通过用户账号查用户")
    @GetMapping("/admin/getAdminByUserName")
    public Admin getAdminByUserName(@RequestParam("userName") String userName) {
        Admin userByUsername = adminService.getUserByUsername(userName);
        List<Role> rolesByAdminId = roleService.getRolesByAdminId(userByUsername.getId());
        userByUsername.setRoles(rolesByAdminId);
        return userByUsername;
    }

//    @ApiOperation("test")
//    @GetMapping("/test")
//    public void test(){
//        String src = ClassUtils.getDefaultClassLoader().getResource("application.yml").getPath();
//        Yaml yaml = new Yaml();
//        FileWriter fileWriter = null;
//        //层级map变量
//        Map<String, Object> springMap, mailMap, resultMap;
//        try {
//            //读取yaml文件，默认返回根目录结构
//            resultMap = (Map<String, Object>) yaml.load(new FileInputStream(new File(src)));
//            //get出spring节点数据
//            springMap = (Map<String, Object>) resultMap.get("spring");
//            //get出数据库节点数据
//            mailMap = (Map<String, Object>) springMap.get("mail");
//            //修改数据库url，我这个是封装的参数，你们测试可以写死一个值尝试修改即可
////            dataSourceMap.put("url", connect.getUrl());
////            //登录名
////            dataSourceMap.put("username", connect.getUsername());
//            //密码
//            mailMap.put("username", "2822246084@qq.com");
//            //字符输出
//            fileWriter = new FileWriter(new File(src));
//            //用yaml方法把map结构格式化为yaml文件结构
//            fileWriter.write(yaml.dumpAsMap(resultMap));
//            //刷新
//            fileWriter.flush();
//            //关闭流
//            fileWriter.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new RuntimeException("对不起，yaml文件修改失败！");
//        }
//    }
}
